package utilities;

import gameAppLayer.enums.Constants;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import java.util.ArrayList;

import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;

/*
 * Inputs:  Radius
 * Outputs:  Circular pattern of hex tiles, out to given radius of tiles from center.
 * Output values are in hex coordinates
 * 
 * 
 */
public final class HexTileGen {
	
	
	public static ArrayList<ArrayList<Integer>> genTileLocations(int radius) {
		//http://stackoverflow.com/questions/2049196/generating-triangular-hexagonal-coordinates-xyz
		ArrayList<ArrayList<Integer>> tileLocations = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> currLocation;// = new ArrayList<Integer>();
		
		int tempRadius = (int) (radius + radius*(1-Math.cos(Math.PI/6)));
		
		ArrayList<Double> carts;
		float dist;
		
		for(int i = 0; i < tempRadius; i++)
	    {
	        for(int j = -i; j <= i; j++)
	        for(int k = -i; k <= i; k++)
	        for(int l = -i; l <= i; l++)
	            if(Math.abs(j) + Math.abs(k) + Math.abs(l) == i*2 && j + k + l == 0) {
	                currLocation = new ArrayList<Integer>();
	                currLocation.add(j);
	                currLocation.add(k);
	                currLocation.add(l);
	                
	                if (i >= radius) {
	                	carts = CoordConvert.hex2Cart(currLocation);
	                	dist = FastMath.sqrt((float) (carts.get(0)*carts.get(0) + carts.get(1)*carts.get(1)));
	                	if (dist < radius) {
	                		tileLocations.add(currLocation);
	                	}
	                }
	                else {
	                	tileLocations.add(currLocation);
	                }
	            }
	        
	    }
		return tileLocations;
	}
	
	public static void genTileLocationsOutput(int radius) {
		//http://stackoverflow.com/questions/2049196/generating-triangular-hexagonal-coordinates-xyz
		ArrayList<ArrayList<Integer>> tileLocations = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> currLocation;// = new ArrayList<Integer>();
		
		String path = Constants.dataPath + "//hexLocations.txt";
		
		int tempRadius = (int) (radius + radius*(1-Math.cos(Math.PI/6)));
		
		ArrayList<Double> carts;
		Float dist;
		
		
		try {
			FileWriter fw = new FileWriter(path);
			BufferedWriter bw = new BufferedWriter(fw);
			
			for(int i = 0; i < tempRadius; i++)
		    {
		        for(int j = -i; j <= i; j++)
		        for(int k = -i; k <= i; k++)
		        for(int l = -i; l <= i; l++)
		            if(Math.abs(j) + Math.abs(k) + Math.abs(l) == i*2 && j + k + l == 0) {
		                
		            	currLocation = new ArrayList<Integer>();
		                currLocation.add(j);
		                currLocation.add(k);
		                currLocation.add(l);
		            	
		            	carts = CoordConvert.hex2Cart(currLocation);
	                	dist = FastMath.sqrt((float) (carts.get(0)*carts.get(0) + carts.get(1)*carts.get(1)));		            	
	                	
		            	bw.write(j + "\t");
		            	bw.write(k + "\t");
		            	bw.write(l + "\t");
		            	bw.write(dist.toString() + "\n");
		            	System.out.println(dist);
		            }		        
		    }
			bw.close();
		}
		catch (Exception e) {
			System.out.println("IOException: " + e);
		}
	}
	

}

//alternative version
//int[] start = {0,0,0};
//int[][] deltas = {{1,0,-1},{0,1,-1},{-1,1,0},{-1,0,1},{0,-1,1},{1,-1,0}};
//for(int i = 0; i < radius; i++) {
//	int x = start[0];
//	int y = start[1]-i;
//	int z = start[2]+i;
//	for(int j = 0; j < 6; j++) {
//		for(int k = 0; k < i; k++) {
//			x = x+deltas[j][0];
//			y = y+deltas[j][1];
//			z = z+deltas[j][2];
//			
//			System.out.println("(" +x +"," +y+"," +z+")");
//			
//			currLocation = new ArrayList<Integer>(); //.add(x);
//			currLocation.add(x);
//			currLocation.add(y);
//			currLocation.add(z);
//			
//			tileLocations.add(currLocation);
//			//currLocation.clear();
//		}
//	}
//}
